package com.jarveis.frame.task;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 任务池
 * 
 * @author liuguojun
 */
public final class TaskPool {

	private int minSize;
	private int maxSize;
	private AtomicInteger poolSize = new AtomicInteger(0);
	private TaskQueue taskQueue;
	private TaskMonitor taskMonitor;
	private ArrayList<TaskThread> poolThreads;
	private LinkedList<TaskThread> idleThreads;
	private static TaskPool instance;

	/**
	 * 构造方法
	 * 
	 * @param minSize
	 * @param maxSize
	 */
	private TaskPool(int minSize, int maxSize) {
		this.minSize = minSize;
		this.maxSize = maxSize;

		taskMonitor = new TaskMonitor(this);
		taskMonitor.start();

		taskQueue = new TaskQueue(this);
		taskQueue.start();

		poolThreads = new ArrayList<TaskThread>(maxSize);
		idleThreads = new LinkedList<TaskThread>();
		for (int i = 0; i < this.minSize; i++) {
			TaskThread taskThread = new TaskThread();
			taskThread.start();
			push(taskThread);
			pushIdle(taskThread);
		}
	}

	/**
	 * 获取任务监视器
	 * 
	 * @return TaskMonitor
	 */
	TaskMonitor getTaskMonitor() {
		return taskMonitor;
	}

	/**
	 * 获取最小线程数
	 * 
	 * @return int
	 */
	int getMinSize() {
		return minSize;
	}

	/**
	 * 获取最大线程数
	 * 
	 * @return int
	 */
	int getMaxSize() {
		return maxSize;
	}

	/**
	 * 添加线程
	 * 
	 * @param taskThread
	 */
	void push(TaskThread taskThread) {
		poolThreads.add(taskThread);
		poolSize.incrementAndGet();
	}

	/**
	 * 删除线程
	 * 
	 * @param taskThread
	 */
	void remove(TaskThread taskThread) {
		poolThreads.remove(taskThread);
		poolSize.decrementAndGet();
	}

	/**
	 * 添加空闲线程
	 * 
	 * @param taskThread
	 */
	void pushIdle(TaskThread taskThread) {
		if (!idleThreads.contains(taskThread)) {
			idleThreads.addLast(taskThread);
		}
	}

	/**
	 * 删除空闲线程
	 * 
	 * @param taskThread
	 */
	void removeIdle(TaskThread taskThread) {
		idleThreads.remove(taskThread);
	}

	/**
	 * 通知有空闲线程
	 */
	void notifyIdleTaskThread(TaskThread taskThread) {
		pushIdle(taskThread);
	}

	/**
	 * 是否有空闲线程
	 * 
	 * @return boolean
	 */
	boolean hasIdleTaskThread() {
		return (!idleThreads.isEmpty()) || (poolSize.get() < maxSize);
	}

	/**
	 * 获取空闲线程
	 * 
	 * @return TaskThread
	 */
	TaskThread getIdleTaskThread() {
		TaskThread taskThread = null;

		// 获取空闲的线程
		if (!idleThreads.isEmpty()) {
			taskThread = idleThreads.removeFirst();
			return taskThread;
		}

		// 验证线程池是否已满
		if (poolSize.get() < maxSize) {
			taskThread = new TaskThread();
			taskThread.start();
			push(taskThread);
		}

		return taskThread;
	}

	/**
	 * 执行任务
	 * 
	 * @param task
	 */
	public void runTask(ITask task) {
		if (task.getTaskInfo() != null) {
			task.getTaskInfo().setStatus(ITask.Status.Start);
		}
		taskQueue.addTask(task);
	}

	/**
	 * 获取线程池实例
	 * 
	 * @return TaskPool
	 */
	public static TaskPool getInstance(int minSize, int maxSize) {
		if (instance == null) {
			synchronized (TaskPool.class) {
				if (instance == null) {
					instance = new TaskPool(minSize, maxSize);
				}
			}
		}
		return instance;
	}
}